var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopinfo: [],
    goodslist: [],
    img_url: c.img_url,
    page:1,
    user_id:"0",
    center_bg: c.img_url + '/static/com/xcx_img/vip_img.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    utils.checkLogin();
    this.setData({
      user_id:options.id
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var token = wx.getStorageSync("userinfo").wap_token;
    var data = {
      url: "/zfshop/Shareshop/detailsId",
      token: token,
      user_id:that.data.user_id
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        var data = res.data;
        that.setData({
          shopinfo: data
        });
      }
      that.getgoodslist();
    });
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  ///下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      goodslist: [],
      page: 1,
    });
    this.onShow();
  },
  //上拉加载
  onReachBottom: function () {
    this.getgoodslist();
  },
  //分享
  onShareAppMessage: function (res) {
    if (res.from === "button") {
      var that = this;
      return {
        title: this.data.shopinfo.username,
        path: 'pages/shop/index/index?invitecode=' + wx.getStorageSync("userinfo").invitecode,
        success: function (res) {
          wx.getSystemInfo({
            success: function (d) {
              var token = wx.getStorageSync("userinfo").wap_token;
              console.log(1);
              console.log(1);
              var data = {
                url: "/wsgoods/usershare/add",
                token: token,
                type: 1,
                id: that.data.shopinfo.shop_id
              };
              console.log(3);
              utils.request(data).then(function (res) {
                

              });
            }
          })
        },
      }
    }
  },
  getgoodslist:function(){
    var that = this;
    var token = wx.getStorageSync("userinfo").wap_token;
    var data = {
      url: "/zfshop/Shareshop/goodsLists",
      shop_id: that.data.shopinfo.shop_id,
      token: token,
      page: that.data.page,
      limit: 6,
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        var data = res.data;
        if (data.length > 0) {
          that.setData({
            page: that.data.page + 1
          });
        }
        for (var index in data) {
          data[index].pic = c.img_url + data[index].pic;
        }
        that.setData({
          goodslist: that.data.goodslist.concat(data)
        });
      } else {

      }
      wx.stopPullDownRefresh();
    });
  }

})